package cn.com.zpark.servlet_context;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @PackageName: cn.com.zpark.servlet_context
 * @ClassName: ServletContextDemo04
 * @Description:
 * @author: RZS
 * @date: 2021/11/10  11:27
 */
@WebServlet("/scd4.html")
public class ServletContextDemo04 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取context对象中绑定的数据
        // 获取context对象
        ServletContext context = getServletContext();
        // 获取绑定数据，通过绑定名字获取
        String name = (String) context.getAttribute("name");
        System.out.println(name);
        resp.getWriter().write("<H1>"+name+"</h1>");

        // 移出指定的绑定数据
        context.removeAttribute("name");
    }
}
